import { TaskIdsInit } from "../utils/pathUtils";
import { StateMoal } from "./StateMoal";
import * as DateUtils from "/src/common/utils/DateUtils";
import { type RemoveMethods } from "@/types/remove-methods";
import { FieldContentData } from "@model/fieldTree";

export class TimeLogModel {
  start: number;
  end: number;

  constructor(start: number, end: number) {
    this.start = start;
    this.end = end;
  }

  static computeTakeTime(logs: Array<TimeLogModel>) {
    let takeTime = 0;
    for (let i = 0; i < logs.length; i++) {
      const log = logs[i];
      takeTime = takeTime + log.end - log.start;
    }
    return DateUtils.changeTimeUnit(takeTime);
  }
  static getLastStartEndTime(timeLogModel: Array<TimeLogModel>, lastStartTime: number | undefined, state: string) {
    const timeRange = { startTime: 0, endTime: 0 };
    if (state === "onGonging") {
      if (lastStartTime !== undefined) timeRange.startTime = lastStartTime;
    } else {
      if (timeLogModel.length > 0) {
        timeRange.startTime = timeLogModel[timeLogModel.length - 1].start;
        timeRange.endTime = timeLogModel[timeLogModel.length - 1].end;
      }
    }
    return timeRange;
  }
}

export class TaskIdsModal {
  TaskBased: StateMoal;
  ClockBased: StateMoal;

  constructor(TaskBased: StateMoal, ClockBased: StateMoal) {
    this.TaskBased = TaskBased;
    this.ClockBased = ClockBased;
  }
  static computeFinishRation(task: TaskIdsModal) {
    let finishRatio1 = 0;
    if (task !== undefined && task.TaskBased.finished.length + task.ClockBased.finished.length != 0) {
      finishRatio1 =
        (task.TaskBased.finished.length + task.ClockBased.finished.length) /
        (task.TaskBased.onGonging.length +
          task.ClockBased.onGonging.length +
          task.TaskBased.finished.length +
          task.ClockBased.finished.length +
          task.TaskBased.stopping.length +
          task.ClockBased.stopping.length);
    }
    return Math.floor(finishRatio1 * 100);
  }

  static getStateModal4Task(task: TaskIdsModal | undefined, type: number) {
    if (task === undefined) return undefined;
    if (type === 1) return task.TaskBased;
    if (type === 2) return task.ClockBased;
    return undefined;
  }
  static hasTask(task: TaskIdsModal | undefined) {
    if (task !== undefined) {
      if (
        task.TaskBased.stopping.length +
          task.TaskBased.onGonging.length +
          task.ClockBased.stopping.length +
          task.TaskBased.finished.length +
          task.ClockBased.finished.length +
          task.ClockBased.onGonging.length ===
        0
      )
        return false;
      else return true;
    }
    return false;
  }
  static checkIsAllFinish(task: TaskIdsModal | undefined) {
    if (task !== undefined) {
      if (
        task.TaskBased.stopping.length +
          task.TaskBased.onGonging.length +
          task.ClockBased.stopping.length +
          task.ClockBased.onGonging.length ==
        0
      )
        return true;
    }
    return false;
  }
}

export class StageData {
  id: number;
  name: string;
  lastStartTime: number; // 上次开始时间
  fatherId: number;
  startTime: Array<TimeLogModel>;
  markDownFile: string;
  task: TaskIdsModal;
  finished: boolean;

  constructor(id: number, name: string, lastStartTime: number, fatherId: number) {
    this.id = id;
    this.name = name;
    this.lastStartTime = lastStartTime;
    this.fatherId = fatherId;
    this.startTime = [];
    this.markDownFile = "";
    this.task = TaskIdsInit();
    this.finished = false;
  }

  static changeTask2NewStage(
    newStageId: number,
    oldStageId: number,
    taskId: number,
    type: number,
    state: string,
    stageList: StageList,
  ) {
    const originalStage = stageList.getStageDataById(oldStageId);
    const toSaveStage = stageList.getStageDataById(newStageId);
    if (originalStage !== undefined && toSaveStage !== undefined) {
      const oldStatMoal = TaskIdsModal.getStateModal4Task(originalStage.task, type);
      const newStatMoal = TaskIdsModal.getStateModal4Task(toSaveStage.task, type);
      if (oldStatMoal !== undefined && newStatMoal !== undefined) {
        StateMoal.deleteId(oldStatMoal, taskId, state);
        StateMoal.addId(newStatMoal, taskId, state);
      }
    }
  }
}
export class StageList {
  stageData: Array<StageData>;

  constructor(stageData: Array<StageData>) {
    this.stageData = stageData;
  }

  setInfo(data: RemoveMethods<StageList>) {
    Object.assign(this, data);
  }
  deleteStageDataById(id: number | undefined) {
    this.stageData = this.stageData.filter(value => value.id !== id);
  }
  getStageDataById(id: number | undefined | null) {
    return this.stageData.find(value => value.id === id);
  }

  addStageData(data: StageData) {
    this.stageData.push(data);
  }
  getStageSelectOptions(fieldData: FieldContentData | undefined | null) {
    const result = [];
    if (fieldData !== undefined && fieldData !== null) {
      const stateMoal = fieldData.stageIds;
      const list = new Set<number>();
      StateMoal.getAllId(stateMoal, list, true, true, false);
      for (let i = 0; i < this.stageData.length; i++) {
        const data = this.stageData[i];
        if (list.has(data.id)) {
          result.push({
            label: data.name,
            value: data.id,
          });
        }
      }
    }
    return result;
  }
}
